<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>鼠标移入盒子随机切换翻转</title>
    <style>
        body {
            display: flex;
            flex-direction: column; /* 垂直排列 */
            justify-content: center; /* 水平居中 */
            align-items: center; /* 垂直居中 */
            height: 100vh; /* 使整个视口高度 */
            margin: 0; /* 去掉默认的 margin */
        }

        .position-leftr {
            width: 200px;
            height: 200px;
            perspective: 1000px; /* 设置透视效果 */
        }

        .flip-container {
            width: 100%;
            height: 100%;
            transition: transform 1s ease-in-out; /* 翻转过渡效果 */
            transform-style: preserve-3d; /* 保持3D效果 */
        }

        .flip-front,
        .flip-back {
            position: absolute;
            width: 100%;
            height: 100%;
            backface-visibility: hidden; /* 隐藏背面内容 */
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 20px;
            font-weight: bold;
        }

        .flip-front {
            background-color: skyblue;
        }

        .flip-back {
            background-color: lightcoral; /* 背面颜色 */
            transform: rotateY(180deg); /* 初始状态为翻转180度 */
        }

        .flip-direction {
            margin-top: 20px; /* 与盒子之间的间隔 */
            font-size: 18px; /* 字体大小 */
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="position-leftr">
        <div class="flip-container" id="flipContainer">
            <div class="flip-front">Hover over me!</div>
            <div class="flip-back">Hello, I'm the back!</div>
        </div>
    </div>
    <div class="flip-direction" id="flipDirection">翻转方向: </div>

    <script>
        const flipContainer = document.getElementById('flipContainer');
        const flipDirection = document.getElementById('flipDirection');

        flipContainer.addEventListener('mouseenter', () => {
            // 随机生成 0 到 3 的整数
            const direction = Math.floor(Math.random() * 4);
            let directionText = '';

            // 应用相应的翻转效果
            if (direction === 0) {
                flipContainer.style.transform = 'rotateY(-180deg)'; // 向左翻转
                directionText = '左';
            } else if (direction === 1) {
                flipContainer.style.transform = 'rotateY(180deg)'; // 向右翻转
                directionText = '右';
            } else if (direction === 2) {
                flipContainer.style.transform = 'rotateX(180deg)'; // 向上翻转
                directionText = '上';
            } else {
                flipContainer.style.transform = 'rotateX(-180deg)'; // 向下翻转
                directionText = '下';
            }

            // 更新翻转方向文本
            flipDirection.textContent = `翻转方向: ${directionText}`;
        });

        flipContainer.addEventListener('mouseleave', () => {
            // 鼠标离开时复位
            flipContainer.style.transform = 'rotateY(0deg) rotateX(0deg)';
            flipDirection.textContent = '翻转方向: '; // 清空方向文本
        });
    </script>
</body>
</html>
